package leetcode.editor.cn.q1_300.q150;

/**
 * @author qidi
 * @date 2022-01-18 15:54:26
 */
class Q0151_ReverseWordsInAString {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String reverseWords(String s) {
            StringBuilder ans = new StringBuilder();

            char[] chars = s.toCharArray();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i <= chars.length; i++) {
                if (i == chars.length || chars[i] == ' ') {
                    if (sb.length() > 0) {
                        if (ans.length() != 0) {
                            sb.append(" ");
                        }
                        ans.insert(0, sb);
                        sb.setLength(0);
                    }
                } else {
                    sb.append(chars[i]);
                }
            }

            return ans.toString();
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0151_ReverseWordsInAString().new Solution();
        System.out.println(solution.reverseWords(" asdasd df f"));
        System.out.println(solution.reverseWords("12 "));
        System.out.println(solution.reverseWords("the sky is blue"));
        System.out.println(solution.reverseWords("    hello world    "));
        System.out.println(solution.reverseWords("a good    example"));
        System.out.println(solution.reverseWords("  Bob  Loves  Alice   "));
        System.out.println(solution.reverseWords("Alice does not even like bob"));
    }
}